import 'package:flutter/material.dart';

///动画--AnimationController
class Test6AnimationController extends StatefulWidget {
  const Test6AnimationController({Key? key}) : super(key: key);

  @override
  _Test6AnimationControllerState createState() => _Test6AnimationControllerState();
}

class _Test6AnimationControllerState extends State<Test6AnimationController>
    with SingleTickerProviderStateMixin {
  ///动画控制器
 late AnimationController _controller;

  ///宽高值
  double value = 0;

  @override
  void initState() {
    super.initState();
    //初始化 动画控制器
    // init1();
    init2();
  }

  void init1() {
    _controller = AnimationController(
        vsync: this,
        lowerBound: 0.0, //动画开始值
        upperBound: 1.0, //动画结束值
        duration: Duration(milliseconds: 500) //动画执行时间
        )

      //监听动画执行
      ..addListener(() {
        setState(() {
          value = 50 + 200 * (_controller.value);
          print("zdh-------------- value: $value");
        });
      });
  }

  void init2() {
    _controller = AnimationController(
        vsync: this,
        lowerBound: 0.0, //动画开始值
        upperBound: 150.0, //动画结束值
        duration: Duration(milliseconds: 500) //动画执行时间
        )
      //监听动画执行
      ..addListener(() {
        setState(() {
          value = _controller.value;
          print("zdh-------------- value: $value");
        });
      });
  }

  @override
  void dispose() {
    super.dispose();
    //销毁 动画控制器
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AnimationController"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            MaterialButton(
              color: Colors.amber,
              onPressed: () {
                //执行动画
                if (_controller.isDismissed) {
                  _controller.forward(); //开始
                } else {
                  _controller.reverse(); //反向
                }
              },
              child: Text("点击"),
            ),
            Container(
              width: value,
              height: value,
              color: Colors.blue,
            )
          ],
        ),
      ),
    );
  }
}
